//
//  CoreImageCtr.m
//  iOSDemo
//
//  Created by mokai on 14/11/13.
//  Copyright (c) 2014年 mokai. All rights reserved.
//

#import "CoreImageCtr.h"
@interface CoreImageCtr()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) CIContext *ct;
@property (weak, nonatomic) IBOutlet UILabel *allFilterLab;

@end
@implementation CoreImageCtr


-(void)viewDidLoad{
    [super viewDidLoad];
    //1、创建一个非实时的图像上下文
    _ct = [CIContext contextWithOptions:nil];
    
    //以下是基于GPU的Context
//    _glContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
//    if (!_glContext) {
//        NSLog(@"Failed to create ES context");
//    }
//    _context = [CIContext contextWithEAGLContext:_glContext];
    
    NSArray *filters = [CIFilter filterNamesInCategory:nil];
    _allFilterLab.text = filters.description;
    NSLog(@"%@",_allFilterLab.text);
}
- (IBAction)resetImageClick:(id)sender {
    _imageView.image = [UIImage imageNamed:@"mm1.jpg"];
}

- (IBAction)easyFilterClick:(id)sender {
    CIImage *image = [CIImage imageWithCGImage:_imageView.image.CGImage];//2、
    
    
    CIFilter *easyFilter = [CIFilter filterWithName:@"CISepiaTone"];//3、
    [easyFilter setValue:image forKey:kCIInputImageKey];
    [easyFilter setValue:@0.8f forKey:kCIInputIntensityKey];

    image = [easyFilter valueForKey:kCIOutputImageKey];//4、获得output图像，其输出是一个如何处理图像的处方，并没有实际被渲染。
    CGRect extent = [image extent];
    
    CGImageRef cgImg = [_ct createCGImage:image fromRect:extent];//5、 渲染CIImage到CGImage。
    _imageView.image = [UIImage imageWithCGImage:cgImg];
    
}

@end
